<?php

    include(BASE.'external/facebooksdk/src/facebook.php');
    class Facebook_login extends Facebook
    {
        public $_profile=null;
        function do_api()
        {

        }
        function check_login()
        {
            //fb_login($fb_id)
            global $user;
            if($user&&$user->isLoggedIn())
            {
                expSession::set('fb_login',1);
                return true;
            }
            
            
            $this->setAccessToken($app_token);
            
            user::fb_login($this->getUser());
             if ($user->isLoggedIn())
             {
                 expSession::set('fb_login',1);
                 return true;
             }
             else
             return false;
           
            
        }
        function add_user_form()
        {
             $u= new user();
             if($_profile==null)
            {                //Get profiles
                $_profile = $this->api('/me');
            }
            $u->email=$_profile['email'];
            $u->firstname=$_profile['first_name'];
            $u->lastname=$_profile['last_name'];
            $u->username=$_profile['username'];
           // print_r(json_encode($u));
            return $u;
        }
        function add_user($params)
        {
            global $db;
            if($_profile==null)
            {                //Get profiles
                $_profile = $this->api('/me');
            }
                     
           
            $params['email']=$_profile['email'];
             $params['fb_id']=$_profile['id'];
             $params['fb_token']=$this->getAccessToken();
             $params['created_on']=time();
             $u = new user($params);  
            $ret = $u->setPassword($this->params['pass1'], $this->params['pass2']);
                if ($ret != true) expValidator::failAndReturnToForm($ret, $this->params);
            $u->save();
            //$u->update($params);
            echo $u->id;
            
            $_group=$db->selectObject('groupmembership','member_id='.$u->id.' AND group_id='.FB_DEFAULT_GROUP);
            if($_group==null)
            {
                $_group->member_id=$u->id;
                $_group->group_id=FB_DEFAULT_GROUP;
                $_group->is_admin=0;
                $db->insertObject($_group,'groupmembership');
            }
                
            
            if ($u->id == $id) {
                $_SESSION[SYS_SESSION_KEY]['user'] = $u;
                $user = $u;
            }

        }

    }
?>
